<template>
  <view class="box">
    <my-search @click="gotoSearch"></my-search>
    <view class="scroll-view-container">
    <!-- 左侧导航栏 -->
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh+'px'}">
        <block v-for="(item,i) in cateList" key="i">
        <view :class="['left-scroll-view-item',i==active?'active':'']" @click="activeChange(i)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧导航数据展示 -->
      <scroll-view class="right-scroll-view" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
          <view class="right-scroll-view-item" v-for="(item,i) in catelevel2">
            <!-- 标题区域 -->
            <view class="title">
              /{{item.cat_name}}/
            </view>
            <!-- 列表数据 -->
            <view class="level3-list">
              
            <view class="level3-item" v-for="(item3,index) in item.children" :key="index" @click="toGoods_list(item3)" >
              <image lazy-load="true" :src="item3.cat_icon.replace('dev','web')"  @error="imageError($event,index,i)"></image>
              <view class="text">
                {{item3.cat_name}}
              </view>
            </view>
            </view>
          </view>
        </scroll-view>
    </view>  
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        //窗口可用高度
        wh:0,
        //分类导航数据
        cateList:[],
        active:0,
        //二级分类列表
        catelevel2:[],
        //滚动条位置
        scrollTop: 0
      }
    },
    onLoad() {
      // var that = this   将this赋给that  解决闭包问题
       uni.getSystemInfo({
         success:(res)=> {
            // 可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度 - 自定义的search组件高度
            this.wh = res.windowHeight-50
         }
       })
       this.getCateList()
    },
    methods: {
      async getCateList(){
       const {data:res}  = await uni.$http.get('/api/public/v1/categories')
       if(res.meta.status !== 200) return uni.$showMsg()
       console.log(res.message);
       this.cateList = res.message
       //为二级分类赋值
       this.catelevel2 = res.message[0].children
      },
      activeChange(i){
        this.active = i
        //为二级分类重新赋值
        this.catelevel2 = this.cateList[i].children
        //更改scrootop值
        this.scrollTop = this.scrollTop? 0 : 1
      },
      toGoods_list(id){
        console.log(id);
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+id.cat_id
        })
      },
      imageError(e, img_index, i) {
        //    var _this = this
        //    var imgChildList = _this.processList[i].attachList
        //   if(imgChildList.length > 0){
        //   imgChildList[img_index].attachUrl = "../../static/imgloaderror.png"
        // }
        },
        gotoSearch(){
          uni.navigateTo({
            url:'/subpkg/search/search'
          })
        }
    }
  }
</script>

<style lang="scss">
.scroll-view-container{
  display: flex;
  .left-scroll-view{
    width: 120px;
    background-color:#f7f7f7;
    .left-scroll-view-item{
      line-height: 60px;
      height: 60px;
      text-align: center;
      font-size: 12px;
      position: relative;
      &.active{
        background-color: #ffffff;
        &::before{
          content: ' ';
          width: 3px;
          height: 30px;
          background-color: red;
          display: block;
          position: absolute;
          top:25%;
          left: 0;
        }
      }
      
    }
  }
  .right-scroll-view{
    background-color: white;
    .right-scroll-view-item{
      margin: 15px 0;
      .title{
        text-align: center;
        font-size: 12px;
        margin:25px 0;
        font-weight: bold;
      }
      .level3-list{
        display: flex;
        flex-wrap: wrap;
        margin: 15px 0;
        image{
          width: 60px;
          height: 60px;
        }
        .level3-item{
          width: 33.3%;
          text-align: center;
          font-size: 12px;
          margin-bottom: 10px;
        }
        
      }
    }
  }
}
</style>
